package com.gamatechno.solodestination.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.gamatechno.solodestination.BuildConfig;

/**
 * Utility class
 * 
 * @author pratama
 */
public class Utils {

	// set true when launch in emulator
	private static final boolean isEmulator = true;
	private static final String imeiDummy = "358097050092661";

	public static final String PREF_NAME = "solo_pref";
	public static final String IS_GCM_REG = "is_gcm_registered";
	private static SharedPreferences sp;

	/**
	 * Debuggin method
	 * 
	 * @param tag
	 *            TAG Activity;.
	 * @param msg
	 *            Output Message
	 */
	public static void TRACE(String tag, String msg) {
		if (BuildConfig.DEBUG)
			Log.d(tag, msg);
	}

	/**
	 * Get imei device
	 * 
	 * @param ctx
	 *            Context
	 * @return
	 */
	public static String getImei(Context ctx) {
		TelephonyManager tp = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		return (isEmulator) ? imeiDummy : tp.getDeviceId();
	}

	/**
	 * cek internet
	 * 
	 * @param ctx
	 * @return true jika konek ke internet
	 */
	public static boolean isConnectedToInternet(Context ctx) {
		ConnectivityManager conn = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conn != null) {
			NetworkInfo[] info = conn.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public static boolean getDataBooleanFromSP(Context context, String from) {
		sp = context.getSharedPreferences(PREF_NAME, 0);
		return sp.getBoolean(from, false);
	}

	public static boolean setDataBooleanFromSP(Context context, String from) {
		sp = context.getSharedPreferences(PREF_NAME, 0);
		return sp.getBoolean(from, false);

	}

	/**
	 * get Data String from SharedPreferences
	 * 
	 * @param context
	 *            Context apps
	 * @param from
	 *            String from sp
	 * @return
	 */
	public static String getDataStringFromSP(Context context, String from) {
		sp = context.getSharedPreferences(PREF_NAME, 0);
		return sp.getString(from, "");
	}

	/**
	 * set String data into SharedPreferences
	 * 
	 * @param context
	 * @param to
	 * @param data
	 */
	public static void setDataStringToSP(Context context, String to, String data) {
		Utils.TRACE("Utils", "setDataString: " + data);
		sp = context.getSharedPreferences(PREF_NAME, 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(to, data);
		editor.commit();
	}
}
